<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <!--[if lt IE 9]>
    <script type="text/javascript" src="${pageContext.request.contextPath}/lib/html5shiv.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/lib/respond.min.js"></script>
    <![endif]-->
    <link href="${pageContext.request.contextPath}/static/h-ui/css/H-ui.min.css" rel="stylesheet" type="text/css" />
    <link href="${pageContext.request.contextPath}/static/h-ui.admin/css/H-ui.login.css" rel="stylesheet" type="text/css" />
    <link href="${pageContext.request.contextPath}/static/h-ui.admin/css/style.css" rel="stylesheet" type="text/css" />
    <link href="${pageContext.request.contextPath}/lib/Hui-iconfont/1.0.8/iconfont.css" rel="stylesheet" type="text/css" />
    <!--[if IE 6]>
    <script type="text/javascript" src="${pageContext.request.contextPath}/lib/DD_belatedPNG_0.0.8a-min.js" ></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->
    <title>登录 - 投票系统</title>
</head>
<body>

<div class="header"></div>
<div class="loginWraper">
    <div id="" class="registerBox">
        <form class="form form-horizontal" id="registerForm"  method="post">
            <div class="row cl">
                <label class="form-label col-xs-3"><i class="Hui-iconfont">&#xe60d;</i></label>
                <div class="formControls col-xs-8">
                    <input id="phone" name="phone" onBlur="phoneFun(this.value)" type="text" placeholder="手机号码" class="input-text size-L">
                    <span id="messagePhone"></span>
                </div>
            </div>
            
            <br>
            <div class="row cl">
                <label class="form-label col-xs-3"><i class="Hui-iconfont">&#xe60d;</i></label>
                <div class="formControls col-xs-8">
                    <input  class="input-text size-L"style="width:210px;" type="text" id="smsCode" name="smsCode" value=""placeholder="短信验证码">
                    <button id="msg" type="button" style="width:120px; height:41px" >获取短信验证</button>
                </div>
            </div>
            <br>
            <div class="row cl">
                <label class="form-label col-xs-3"><i class="Hui-iconfont">&#xe60d;</i></label>
                <div class="formControls col-xs-8">
                    <input id="userName" name="userName" type="text" placeholder="用户名" class="input-text size-L">
                </div>
            </div>

            <br>
            <div class="row cl">
                <label class="form-label col-xs-3"><i class="Hui-iconfont">&#xe60e;</i></label>
                <div class="formControls col-xs-8">
                    <input id="password" name="password" type="password" placeholder="登录密码" class="input-text size-L">
                </div>
            </div>
            <br>
            <div class="row cl">
                <label class="form-label col-xs-3"><i class="Hui-iconfont">&#xe60e;</i></label>
                <div class="formControls col-xs-8">
                    <input id="cPassword" name="cPassword" onkeyup="validate()" type="password" placeholder="确认密码" class="input-text size-L">
                    <span id="message"></span>
                </div>
            </div>
            <br>
            <div class="row cl">
                <div class="formControls col-xs-8 col-xs-offset-3">
                    <label for="terms">
                        <input type="checkbox" name="terms" id="terms" value="">
                        同意用户使用手册</label>
                </div>
            </div>
            <div class="row cl">
                <div class="formControls col-xs-8 col-xs-offset-3">
                    <input name="" onclick="register();" class="btn btn-success radius size-L" value="&nbsp;注&nbsp;&nbsp;&nbsp;&nbsp;册&nbsp;">&nbsp;&nbsp;
                    <input name="" type="reset" class="btn btn-default radius size-L" value="&nbsp;取&nbsp;&nbsp;&nbsp;&nbsp;消&nbsp;">
                </div>
            </div>
            <div class="row cl">
                <div class="formControls col-xs-8 col-xs-offset-3">
                    <a href="${pageContext.request.contextPath}/login/toLoginJsp" style="">有账号？去登录</a>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="footer">Copyright © 2020 投票系统v1.0. All Rights Reserved.</div>
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript">
    function register() {
        //获取表单对象
        var registerForm = document.getElementById("registerForm");
        if(registerForm.phone.value==""){
            alert("手机号码不能空，请输入");
            return false;
        }
        if(registerForm.smsCode.value==""){
            alert("短信验证码不能为空，请输入");
            return false;
        }
        //判断用户名密码
        if(registerForm.userName.value==""){
            alert("用户名为空，请输入");
            return false;
        }
        if(registerForm.password.value==""){
            alert("密码为空，请输入");
            return false;
        }
        if(registerForm.cPassword.value==""){
            alert("确认密码为空，请输入");
            return false;
        }
        if(registerForm.password.value!=registerForm.cPassword.value){
            alert("两次密码不相同，请重新输入");
            return false;
        }
        if(!$('#terms').prop('checked')){
            alert("请勾选条款");
            return false;
        }
        //跳转页面 
        $.ajax({
            type: "post",
            url: "${pageContext.request.contextPath}/register/saveRegister",
            data: $("#registerForm").serialize(),
            dataType: "json",
            success: function(data){
                alert(data.responseText);
                $(window).attr("location","${pageContext.request.contextPath}/login/goLoginJsp");
            },
            error:function(data){
                alert(data.responseText);
                if(data.responseText=='registration success'){
                    $(window).attr("location","${pageContext.request.contextPath}/login/goLoginJsp");
                }
            }
        });
    }

    //校验手机号码
    function phoneFun(phones){
        var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
        if (!myreg.test(phones)) {
            document.getElementById("messagePhone").innerHTML="<font color='red'>手机号码不正确</font>";
            return false;
        }else{
            document.getElementById("messagePhone").innerHTML="<font color='green'>手机号码正确</font>";
            return true;
        }
    }

    function validate() {
        var pwd1 = document.getElementById("password").value;
        var pwd2 = document.getElementById("cPassword").value;
        <!-- 对比两次输入的密码 -->
        if(pwd1 == pwd2){
            document.getElementById("message").innerHTML="<font color='green'>两次密码相同</font>";
        }else {
            document.getElementById("message").innerHTML="<font color='red'>两次密码不相同</font>";
        }
    }
</script>
<script type="text/javascript">
    var count=60,timer=null;
    $("#msg").click(function(){
        if($("#phone").val()==''){
            alert("请输入手机号码");
            return false;
        }
        if(!phoneFun($("#phone").val())){
            alert("请输入正确的手机号码");
            return false;
        }
        $.ajax({
            type: "post",
            url: "${pageContext.request.contextPath}/register/sendSMS",
            data: {phone:$("#phone").val()},
            dataType: "json",
            success: function(data){
                alert(data.responseText)
            },
            error:function(data){
                alert(data.responseText)
            }
        });
        /*防止用户重复点击按钮*/
        if(timer==null){
            timer=setInterval(function(){count--;$("#msg").text(count+"秒后获取验证码");
                if(count<=0){clearInterval(timer);$("#msg").text("点击获取验证码");timer=null;}},1000);
        }
    });
</script>
</body>
</html>